package com.linjiahao.nettyOne.handler.Impl;

import com.alibaba.fastjson.JSON;
import com.linjiahao.nettyOne.constant.HandlerServiceName;
import com.linjiahao.nettyOne.handler.AbstractHandle;
import com.linjiahao.nettyOne.handler.HandlerServiceFactory;
import com.linjiahao.nettyOne.service.ChatListService;
import com.linjiahao.servicebase.vo.NettyMessageVO;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @Author Shinelon
 * @Date 2022/1/12 22:42
 * @Version 1.0
 */
@Service
@Slf4j
public class AddChatListHandlerImpl implements AbstractHandle, InitializingBean {

    @Autowired
    private ChatListService chatListService;

    @Override
    public String getServiceTypeName() {
        return HandlerServiceName.ADD_CHAT_LIST.getName();
    }

    @Override
    public Object handlerWebsocket(ChannelHandlerContext ctx, Object message) {
        NettyMessageVO nettyMessageVO = (NettyMessageVO)message;
        String success = chatListService.addChatList(nettyMessageVO);
        nettyMessageVO.setData(success);
        ctx.channel().writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(nettyMessageVO)));
        return null;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        HandlerServiceFactory.register(getServiceTypeName(),this);
    }
}
